var startTime=echartUtil.GetDateStr(-90);
var endTime=echartUtil.GetDateStr(0);
var tableReport;
var secondApprovalTable;
//相关业务逻辑
$(function(){
	report.getAllSearchWhere();
	report.getAllMachining();
//	report.getRoportNotApprovalData();
	report.reportBind();
	report.getWorkshopName();
	report.bind();
	$(".reportAgreeAll").hide();
	$(".reportReturnAll").hide(); 
	$(".selectAll").hide(); 
	$(".selectNotAll").hide(); 
	$("#startTime").val(startTime);
	$("#endTime").val(endTime);
});


var report={//页面总变量
		reportId:"",
		orderPamar:['companyOrderId','factoryId','coustomId','coustomOrderId','orderId'],
		orderList:[],
		typeMach:"",
		bind:function(){
			/**
			 * 导出为excel
			 */
			$("#download").bind("click",function(){
				var object = new Object();
				for(var i=0;i<report.orderPamar.length;i++){
					object[report.orderPamar[i]]=$("#"+report.orderPamar[i]).val();
				}
				object.machining=$("#machining").val();
				object.status=$("#status option:selected").val();
				object.startTime=$("#startTime").val();
				object.endTime=$("#endTime").val();
				object.workShopId=$("#workshopName").val(); 
				object.type =8;
				var url="approval/downloadReportData?";
				for(var key in object){
					url+=key+"="+object[key]+"&";
				}
				tableUtil.download(url);
			});
			
			
			/**
			 * 查询我的报工记录按钮
			 */
			$("#querryReportBtn").bind("click",function(){
				var object=new Object;
				for(var i=0;i<report.orderPamar.length;i++){
					object[report.orderPamar[i]]=$("#"+report.orderPamar[i]).val();
				}
				object.machining=$("#machining").val();
				object.status=$("#status option:selected").val();
				object.startTime=$("#startTime").val();
				object.endTime=$("#endTime").val();
				object.workShopId=$("#workshopName").val();
				console.log(object);
				report.getRoportNotApprovalData(object);
				report.getSecondRoportApprovalData(object);
			});
			
			
			
			
			for(var i=0;i<report.orderPamar.length;i++){
				$("#"+report.orderPamar[i]).bind("change",function(){
					filterInput.filterView("",report.orderPamar,report.orderList);
				});
			}
			
			
			
			$(".reportAll").bind("click",function(){
				$("#reportApproval  .check").show();
				$("#reportApproval  .oneReportAgree").hide();
				$("#reportApproval  .oneReportReturn").hide();
				$(this).nextAll().eq(0).show();
				$(this).nextAll().eq(1).show();
				$(this).nextAll().eq(2).show();
				$(this).nextAll().eq(3).show();
			});
			
			$(".selectAll").bind("click",function(){
				$("#reportApproval  input[name='reportCheck']").prop("checked",true);
			});
			
			$(".selectNotAll").bind("click",function(){
				$("#reportApproval  input[name='reportCheck']").prop("checked",false);
			});
			
			$("#reportReturnBtn").bind("click",function(){
				var reason=$("#reportReturnReason").val();
				if(reason==''){
					layAlert("请输入退回理由");
					return false;
				}
				ajaxUtil.ajaxRuquest("approval/approvalReport",{"id":report.reportId,"approvalReason":reason,"status":2},function(data){
					$("#reportReasonModel").modal("hide");
					report.reportId="";
					layer.msg(data,{icon:1});
					tableReport.ajax.reload(null, false);
					secondApprovalTable.ajax.reload( null, false);
				});
			});
			
			$(".reportAgreeAll").bind("click",function(){
	    		var idArray=new Array(); 
	    		var outArray=new Array();
		    	$("#reportApproval  [name='reportCheck']:checked").each(function(){  
		    		var object=new Object();
		    		object.typeId=$(this).val();
		    		
		    		var approvalIsCanUpdateReportPrice = $(this).attr('approvalIsCanUpdateReportPrice');
		    	    //公司设置可以修改单价
		    		if(approvalIsCanUpdateReportPrice=='1'){
		    			var unitPrice=$(this).parent().parent().children("td").eq(13).text()
		    			object.unitPrice=unitPrice;
		    		}
		    		idArray.push(object);  
		    		var procedure=$(this).parent().parent().children("td").eq(7).text();
		    		if(procedure=='交接出库'||procedure=='交接至客户'){
		    			outArray.push(procedure);
		    		}
		    	});
		    	if(outArray.length>0){
		    		layAlert("出库工序请在接收部门审核");
					return false;
		    	}
		    	if(idArray.length<=0){
		    		return false;
		    	}
	    		ajaxUtil.ajaxRuquest('approval/allApprovalReport',{"id":JSON.stringify(idArray),"approvalReason":'审批通过',"status":1},function(data){
	    			layer.msg(data,{icon:1});
	    			tableReport.ajax.reload( null, false);
	    		});
		    	$(this).hide();
		    	$(this).nextAll().eq(0).hide();
		    	$(this).nextAll().eq(1).hide();
		    	$(this).nextAll().eq(2).hide();
			});
			
			$("#reportReturnAllBtn").bind("click",function(){
				var reason=$("#reportReturnReason").val();
				if(reason==''){
					layAlert("请输入退回理由");
					return false;
				}
				ajaxUtil.ajaxRuquest('approval/allApprovalReport',{"id":report.reportId,"approvalReason":reason,"status":2},function(data){
					$(".reportAgreeAll").hide();
					$(".reportReturnAll").hide(); 
					$(".selectAll").hide(); 
					$(".selectNotAll").hide(); 
					$("#reportReasonModel").modal("hide");
					report.reportId="";
					layer.msg(data,{icon:1});
					tableReport.ajax.reload(null, false);
				   }
				);
			});
			
			$(".reportReturnAll").bind("click",function(){
	    		var idArray=new Array(); 
	    		var outArray=new Array();
		    	$("#reportApproval  [name='reportCheck']:checked").each(function(){  
		    		var object=new Object();
		    		object.typeId=$(this).val();
		    		idArray.push(object);  
		    		var procedure=$(this).parent().parent().children("td").eq(7).text();
		    		if(procedure=='交接出库'||procedure=='交接至客户'){
		    			outArray.push(procedure);
		    		}
		    	});  
		    	if(outArray.length>0){
		    		layAlert("出库工序请在接收部门审核");
					return false;
		    	}
		    	if(idArray.length<=0){
		    		return false;
		    	}
	    		report.reportId=JSON.stringify(idArray);
		    	$("#reportReasonModel").modal("show");
		    	$("#reportReturnBtn").hide();
		    	$("#reportReturnAllBtn").show();
		    	$(this).prevAll().eq(0).hide();
	    		$(this).hide();
		    	$(this).nextAll().eq(0).hide();
		    	$(this).nextAll().eq(1).hide();
			});
		},
		getWorkshopName: function () {
			ajaxUtil.ajaxRuquest('basicData/getDeptNoMegerWorkShop', {
				"deptId": null
			}, function (data) {
				var html = "";
				for (var i = 0; i < data.length; i++) {
					html = html + "<option value='" + data[i].id + "'>" + data[i].workshopName + "</option>";
				}
				$("#workshopName").html("<option value=''>请选择</option>");
				$("#workshopName").append(html);
			});
		},
		getAllSearchWhere:function(){/**获取所有筛选条件*/
			ajaxUtil.ajaxRuquestNoQuan('companyMaterial/getAllOrderSearchWhere',null,function(data){
				report.orderList=data;
				filterInput.filterView("",report.orderPamar,report.orderList);
			});
		},
		getAllMachining:function(){
			ajaxUtil.ajaxRuquestNoQuan('basicData/getRowMaterialType',{'isvolid':1},function(data){
				var html="";
				for(var i=0;i<data.length;i++){
					html+="<option value='"+data[i]+"' >";
				}
				$("#machiningList").html(html);
			});
		},
		reportBind:function(){
			//报工审核
			$('#reportApproval tbody').on('click', '.oneReportAgree', function () {
				var procedure=$(this).parent().parent().children("td").eq(6).text();
				if(procedure=='交接出库' || procedure=='交接至客户'){
					layAlert("出库工序请在接收部门审核");
					return false;
				}
				var object=new Object();
				object.id=$(this).val();
				object.approvalReason='审批通过';
				object.status=1;
				var approvalIsCanUpdateReportPrice = $(this).attr('approvalIsCanUpdateReportPrice');
	    	    //公司设置可以修改单价
	    		if(approvalIsCanUpdateReportPrice=='1'){
	    			var unitPrice=$(this).parent().parent().children("td").eq(13).text()
	    			console.log(unitPrice);
	    			if(unitPrice==''){
	    				unitPrice=0;
	    			}
	    			object.unitPrice=unitPrice;
	    		}
				ajaxUtil.ajaxRuquest("approval/approvalReport",object,function(data){
					layer.msg(data,{icon:1});
					tableReport.ajax.reload( null, false);
				});				
			});
			//报工退回
			$('#reportApproval tbody').on('click', '.oneReportReturn', function () {
				var procedure=$(this).parent().parent().children("td").eq(6).text();
				if(procedure=='交接出库' || procedure=='交接至客户'){
					layAlert("出库工序请在接收部门审核");
					return false;
				}
				//关联源
				var releationSource=$(this).parent().parent().children("td").eq(8).text();
				console.log(releationSource);
				if($(this).text()=='同意' && (releationSource=='退回至仓库' || releationSource=='外协' || releationSource=='成品仓' || releationSource=='待处理仓' || releationSource=='报废仓' || releationSource=='半成品')){
					layAlert("仓库与外协不可申请退回");
					return false;
				}
				var id=$(this).val();
				var typeMach=$(this).parent().parent().children("td").eq(4).text();
				report.typeMach=typeMach;
				report.reportId=id;
				$("#reportReasonModel").modal("show");
				$("#reportReturnBtn").show();
		    	$("#reportReturnAllBtn").hide();
			});
			
			
			
			
			//报工审核
			$('#secondReportApproval tbody').on('click', '.oneReportAgree', function () {
				var object=new Object();
				object.id=$(this).val();
				object.approvalReason='审批通过';
				object.status=1;
				var approvalIsCanUpdateReportPrice = $(this).attr('approvalIsCanUpdateReportPrice');
				//公司设置可以修改单价
				if(approvalIsCanUpdateReportPrice=='1'){
					var unitPrice=$(this).parent().parent().children("td").eq(13).text()
					object.unitPrice=unitPrice;
				}
				ajaxUtil.ajaxRuquest("approval/approvalReport",object,function(data){
					layer.msg(data,{icon:1});
					secondApprovalTable.ajax.reload( null, false);
				});				
			});
			//二次报工退回
			$('#secondReportApproval tbody').on('click', '.oneReportReturn', function () {
				var id=$(this).val();
				var typeMach=$(this).parent().parent().children("td").eq(4).text();
				report.typeMach=typeMach;
				report.reportId=id;
				$("#reportReasonModel").modal("show");
				$("#reportReturnBtn").show();
				$("#reportReturnAllBtn").hide();
			});
			//拒绝修改申请
			$('#reportApproval tbody').on('click', '.notLaunch', function () {
				var id=$(this).val();
				ajaxUtil.ajaxRuquest("workshop/notLaunchReport",{"id":id},function(data){
					layer.msg(data,{icon:1});
					tableReport.ajax.reload( null, false);
				});
			});
		},
		getRoportNotApprovalData:function(object){
			object.type=8;
			tableReport=ajaxUtil.dataTablesAjaxNotFxiaction("reportApproval",10,"approval/getRoportNotApprovalData",
					object,[
					{"data":"typeId",render: function (data,type,row,meta) {
						if(row.procedur=='交接出库' || row.procedur=='交接至客户' ){
							return "";
						}else{
							if(row.status==0){
								if(row.firstApprovalStatus!=1){
									return "<button class='oneReportAgree btn btn-secondary radius' type='button' value='"+data+"' approvalIsCanUpdateReportPrice='"+row.approvalIsCanUpdateReportPrice+"' >审核</button> " +
									"<button value='"+data+"'  class='btn btn-danger radius oneReportReturn' approvalIsCanUpdateReportPrice='"+row.approvalIsCanUpdateReportPrice+"' >退回</button>" +
									"<input type='checkbox' name='reportCheck' value='"+data+"' hidden='hidden' class='check' approvalIsCanUpdateReportPrice='"+row.approvalIsCanUpdateReportPrice+"' >";
								}else{
									return "";
								}
							}else if(row.status==1 && row.isLaunch==1){
								return "<button value='"+data+"'  class='btn btn-secondary radius oneReportReturn' approvalIsCanUpdateReportPrice='"+row.approvalIsCanUpdateReportPrice+"' >同意</button> "+
								       "<button value='"+data+"'  class='btn btn-danger radius notLaunch' approvalIsCanUpdateReportPrice='"+row.approvalIsCanUpdateReportPrice+"' >拒绝</button>";
							}else{
								return "";
							}
						}
					}},
					{"data":"companyOrderId"},
					{"data":"factoryId"},
					{"data":"customerId"},
					{"data":"machining"},
					{"data":"color"},
					{"data":"workShopName"},
					{"data":"procedur"},
					{"data":"relationSource"},
					{"data":"touchTime"},
					{"data":"qualified"},
					{"data":"noQualified"},
					{"data":"noQualifiedReason"},
					{"data":"unitPrice"},
					{"data":"touchPeople"},
					{"data":"auditor"},
					{"data":"status",render: function (data,type,row) {
						if(data==0){
							if(row.firstApprovalStatus==1){
								return "已审核,等待二级审核";
							}else{
								return "未审核";
							}
						}
						if(data==1){
							if(row.isLaunch==1){
								return "申请退回";
							}else{
								return "已审核";
							}
						}
						if(data==2){
							return "已退回";
						}
					}}
			],[13],function(td, cellData, rowData, row, col){
				//如果公司设定审核人可以修改报工价格
				if(rowData.approvalIsCanUpdateReportPrice==1){
					if(rowData.status==0){
						$(td).attr("contenteditable",true);
					}
				}else{//不可以修改，则隐藏本列
					if(row==0){
						$(".tiShiSpan").hide();
						$("#reportApproval thead tr th").eq(13).hide();
					}
					$(td).hide();
				}
			});
		},
		getSecondRoportApprovalData:function(object){
			secondApprovalTable=ajaxUtil.dataTablesAjaxNotFxiaction("secondReportApproval",10,"approval/getSecondRoportApprovalData",
					object,[
						{"data":"typeId",render: function (data,type,row,meta) {
							if(row.status==0){
									return "<button class='oneReportAgree btn btn-secondary radius' type='button' value='"+data+"' approvalIsCanUpdateReportPrice='"+row.approvalIsCanUpdateReportPrice+"' >审核</button> " +
									"<button value='"+data+"'  class='btn btn-danger radius oneReportReturn' approvalIsCanUpdateReportPrice='"+row.approvalIsCanUpdateReportPrice+"' >退回</button>" ;
//									"<input type='checkbox' name='reportCheck' value='"+data+"' hidden='hidden' class='check' approvalIsCanUpdateReportPrice='"+row.approvalIsCanUpdateReportPrice+"' >";
							}
//							else if(row.status==1 && row.isLaunch==1){
//								return "<button value='"+data+"'  class='btn btn-secondary radius oneReportReturn' approvalIsCanUpdateReportPrice='"+row.approvalIsCanUpdateReportPrice+"' >同意</button> "+
//								"<button value='"+data+"'  class='btn btn-danger radius notLaunch' approvalIsCanUpdateReportPrice='"+row.approvalIsCanUpdateReportPrice+"' >拒绝</button>";
//							}
							else{
								return "";
							}
					}},
						{"data":"companyOrderId"},
						{"data":"factoryId"},
						{"data":"customerId"},
						{"data":"machining"},
						{"data":"color"},
						{"data":"workShopName"},
						{"data":"procedur"},
						{"data":"relationSource"},
						{"data":"touchTime"},
						{"data":"qualified"},
						{"data":"noQualified"},
						{"data":"noQualifiedReason"},
						{"data":"unitPrice"},
						{"data":"touchPeople"},
						{"data":"firstApprovalUserName"},
						{"data":"firstApprovalTime"},
						{"data":"auditor"},
						{"data":"status",render: function (data,type,row) {
							if(data==0){
								if(row.firstApprovalStatus==1){
									return "已审核,等待二级审核";
								}else{
									return "未审核";
								}
							}
							if(data==1){
								if(row.isLaunch==1){
									return "申请修改";
								}else{
									return "已审核";
								}
							}
							if(data==2){
								return "已退回";
							}
						}}
						],[13],function(td, cellData, rowData, row, col){
				//如果公司设定审核人可以修改报工价格
				if(rowData.approvalIsCanUpdateReportPrice==1){
					if(rowData.status==0){
						$(td).attr("contenteditable",true);
					}
				}else{//不可以修改，则隐藏本列
					if(row==0){
						$("#secondReportApproval thead tr th").eq(13).hide();
					}
					$(td).hide();
				}
			});
		}
}